package com.xinhua.web.servlet;

import com.xinhua.domain.Consumption;
import com.xinhua.util.JDBCUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

//查询全部
@WebServlet("/findServlet")
public class FindServlet extends HttpServlet {
    private QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            int uuid = (int) request.getSession().getAttribute("uuid");
            List<Consumption> query = queryRunner.query("select * from day23.consumption where uuid = ?", new BeanListHandler<Consumption>(Consumption.class), uuid);
            System.out.println(query);
            request.setAttribute("user", query);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        request.getRequestDispatcher("/list.jsp").forward(request, response);


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
